﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LabManager.UserControls.Tabs.Templates;
using DataConnector;
using Library;
using Equin.ApplicationFramework;
using System.Data.Entity;

namespace LabManager.UserControls.Tabs.Masterdata
{
    public partial class TabMachineType : TabTemplate
    {
        private BindingListView<MachineType> bindingList;
        private BindingSource bindingSource = new BindingSource();
        private int selectedID = -1;

        public TabMachineType()
        {
            InitializeComponent();
        }

        public override void Load()
        {
            SetTexts();
            LoadMachineTypeList();
        }

        private void SetTexts()
        {
            //TODO
        }

        private void LoadMachineTypeList()
        {
            bindingList = new BindingListView<MachineType>(DataConnectorLayer.Instance.R_MachineType.FindAll(x => x.MachineTypeID > 0).ToList<MachineType>());
            bindingSource.DataSource = bindingList;
            dgMain.DataSource = bindingSource;
            bnNavi.BindingSource = bindingSource;

            if (dgMain.Rows.Count > 0)
            {
                selectedID = (int)dgMain.Rows[0].Cells[0].Value;
            }

            dgMain_SelectionChanged(this, null);
        }

        #region Events

        private void dgMain_SelectionChanged(object sender, EventArgs e)
        {
            if (dgMain.SelectedRows.Count > 0)
            {
                var selectedRow = dgMain.SelectedRows[0];

                selectedID = (int)selectedRow.Cells[0].Value;
                tbIdentification.Text = selectedRow.Cells[1].Value.ToString();
                tbManufacturer.Text = selectedRow.Cells[2].Value.ToString();
                tbDescription.Text = selectedRow.Cells[3].Value.ToString();

                // Delete button 
                var canDeleteProductMachineCompatibility = DataConnectorLayer.Instance.R_ProductMachineCompatibility.FindOne(x => x.MachineType.MachineTypeID == selectedID) == null;
                var canDeleteMachine = DataConnectorLayer.Instance.R_Machine.FindOne(x => x.MachineType.MachineTypeID == selectedID) == null;
                btDelete.Enabled = canDeleteProductMachineCompatibility && canDeleteMachine;
                btUpdate.Enabled = true;
            }
            else
            {
                tbIdentification.Text = string.Empty;
                tbManufacturer.Text = string.Empty;
                tbDescription.Text = string.Empty;

                btUpdate.Enabled = false;
                btDelete.Enabled = false;
            }
        }

        private void btNew_Click(object sender, EventArgs e)
        {
            var item = new MachineType() { Identification = Helper.EMPTY_STRING, Manufacturer = Helper.EMPTY_STRING, Description = Helper.EMPTY_STRING };
            DataConnectorLayer.Instance.R_MachineType.Create(item);
            DataConnectorLayer.Instance.R_MachineType.SaveChanges();
            LoadMachineTypeList();
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_MachineType.FindById(selectedID);
            item.Identification = tbIdentification.Text != "" ? tbIdentification.Text : Helper.EMPTY_STRING;
            item.Manufacturer = tbManufacturer.Text != "" ? tbManufacturer.Text : Helper.EMPTY_STRING;
            item.Description = tbDescription.Text != "" ? tbDescription.Text : Helper.EMPTY_STRING;
            DataConnectorLayer.Instance.R_MachineType.Update(item);
            DataConnectorLayer.Instance.R_MachineType.SaveChanges();

            LoadMachineTypeList();
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            var item = DataConnectorLayer.Instance.R_MachineType.FindById(selectedID);
            DataConnectorLayer.Instance.R_MachineType.Delete(item);
            DataConnectorLayer.Instance.R_MachineType.SaveChanges();

            LoadMachineTypeList();
        }

        private void tbFilter_TextChanged(object sender, EventArgs e)
        {
            var search = tbFilter.Text.ToLower();

            bindingList.ApplyFilter(delegate(MachineType machineType)
            {
                return machineType.Identification.ToLower().Contains(search) ||
                       machineType.Manufacturer.ToLower().Contains(search) ||
                       machineType.Description.ToLower().Contains(search);
            });
        }

        #endregion

    }
}